package com.jintoufs.web.action.baseInfo;

import com.alibaba.fastjson.JSON;
import com.jintoufs.domain.baseinfo.Site;
import com.jintoufs.domain.baseinfo.SiteCoin;
import com.jintoufs.domain.constants.Constants;
import com.jintoufs.logAnnotation.SystemControllerLog;
import com.jintoufs.service.baseinfo.CoinService;
import com.jintoufs.service.baseinfo.SiteCoinService;
import com.jintoufs.service.baseinfo.SiteService;
import com.jintoufs.web.action.support.SupportAction;
import org.cosmos.modules.web.msg.AjaxMsg;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoemo
 */
@Controller
@Scope("prototype")
@RequestMapping("/site")
public class SiteAction extends SupportAction {
    @Resource
    private SiteService siteService;
    @Resource
    private CoinService coinService;
    @Resource
    private SiteCoinService siteCoinService;

    @RequestMapping("/list")
    public String list(Model model) throws IOException {
        List<Map<String, Object>> list = siteService.queryByParams(null);
        model.addAttribute("listEntity", list);
        return "baseinfo/site-list";
    }

    @RequestMapping("/oper")
    @SystemControllerLog(description = "修改网站")
    @ResponseBody
    public AjaxMsg oper(String oper, Site site) {
        AjaxMsg ajaxMsg = new AjaxMsg();
        try {
            if (oper.equals("edit")) {
                siteService.updateSite(site);
                ajaxMsg.setCode("200");
                ajaxMsg.setMessage("更新成功");
            } else if (oper.equals("add")) {
                siteService.insert(site);
                ajaxMsg.setCode(AjaxMsg.SUCCESS);
                ajaxMsg.setMessage("添加成功");
            } else {
                ajaxMsg.setCode(AjaxMsg.FAILURE);
                ajaxMsg.setMessage("操作代码错误！系统只提供增加/修改/删除功能。");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxMsg.setCode(AjaxMsg.FAILURE);
            ajaxMsg.setMessage("添加失败");
        }
        return ajaxMsg;
    }

    /**
     * input.do?id= 进入类目编辑页面 <br>
     * input.do 进入类目新增页面（不包含选中父类目）
     *
     * @param id    类目Id
     * @param model
     * @return vm/category/input.vm
     */
    @RequestMapping("input")
    public String input(Integer id, Model model) {
        Site result = null;
        List<Map<String, Object>> coins = null;
        String title;

        // 是否为编辑保存
        String isEdit;

        // 修改
        if (id != null && id != 0) {
            title = "修改";
            result = siteService.selectByPrimaryKey(id);
            Map<String, Object> params = new HashMap();
            params.put("siteId", id);
//            coins = coinService.queryByParams(null);
//            List<Map<String, Object>> coins2 = coinService.querySiteCoinByParams(params);
//            for (Map coin : coins) {
//                for (Map tmp : coins2) {
//                    if (tmp.get("code").equals(coin.get("code"))) {
//                        coin.put("status", 1);
//                    }
//                }
//            }
            isEdit = "edit";
//            model.addAttribute("coins", coins);
        } else {
            title = "新增";
            isEdit = "add";
            coins = coinService.queryByParams(null);
            // model.addAttribute("coins", coins);
        }

        model.addAttribute("isEdit", isEdit);
        model.addAttribute("result", result);

        model.addAttribute("title", title);

        return "baseinfo/site-input";
    }

    @RequestMapping("/delete")
    @SystemControllerLog(description = "删除网站")
    public void delete(HttpServletResponse response, Integer[] ids) {
        for (Integer id : ids) {
            siteService.deleteByPrimaryKey(id);
        }
        writeInfo(response, JSON.toJSONString(new AjaxMsg()));
    }

    @RequestMapping("/updateSiteCoinsRechargeStatus")
    @SystemControllerLog(description = "修改网站币种状态")
    @ResponseBody
    public AjaxMsg updateSiteCoinsRechargeStatus(HttpServletResponse response, Integer id) {
        SiteCoin siteCoin = siteCoinService.selectByPrimaryKey(id);
        if ("1".equals(siteCoin.getRechargeStatus())) {
            siteCoin.setRechargeStatus(Constants.SITE_COIN_STATUS_ENABLE);
        } else {
            siteCoin.setRechargeStatus(Constants.SITE_COINS_STATUS_DISABLE);
        }
        siteCoinService.updateByPrimaryKey(siteCoin);
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setDatas(siteCoin);
        return ajaxMsg;
    }

    @RequestMapping("/updateSiteCoinsWithDrawStatus")
    @SystemControllerLog(description = "修改网站币种状态")
    @ResponseBody
    public AjaxMsg updateSiteCoinsWithDrawStatus(HttpServletResponse response, Integer id) {
        SiteCoin siteCoin = siteCoinService.selectByPrimaryKey(id);
        if ("1".equals(siteCoin.getWithdrawStatus())) {
            siteCoin.setWithdrawStatus(Constants.SITE_COIN_STATUS_ENABLE);
        } else {
            siteCoin.setWithdrawStatus(Constants.SITE_COINS_STATUS_DISABLE);
        }
        siteCoinService.updateByPrimaryKey(siteCoin);
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setDatas(siteCoin);
        return ajaxMsg;
    }

    /**
     * @param id    类目Id
     * @param model
     */
    @RequestMapping("getSiteCoins")

    public String getSiteCoins(Integer id, Model model) {
        List<Map<String, Object>> coins = null;
        if (id != null && id != 0) {
            Map<String, Object> params = new HashMap();
            params.put("siteId", id);
            coins = siteCoinService.queryByParams(params);
            model.addAttribute("coins", coins);
            Site site = siteService.selectByPrimaryKey(id);
            model.addAttribute("site", site);
        }
        return "/baseinfo/site-coins";
    }

    /**
     */
    @RequestMapping("updateSiteCoin")
    @ResponseBody
    public AjaxMsg updateSiteCoin(Integer id,String coinRename,String priceApi) {
        SiteCoin siteCoin=siteCoinService.selectByPrimaryKey(id);
        siteCoin.setCoinRename(coinRename);
        siteCoin.setPriceApi(priceApi);
        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.FAILURE);
        if (siteCoin != null) {
            siteCoinService.updateByPrimaryKey(siteCoin);
            ajaxMsg.setCode(AjaxMsg.SUCCESS);
            ajaxMsg.setMessage("更新成功");
        } else {
            ajaxMsg.setMessage("更新失败");
        }
        return ajaxMsg;
    }

    /**
     * @param ids
     */
    @RequestMapping("deleteSiteCoin")
    @ResponseBody
    public AjaxMsg deleteSiteCoin(String[] ids) {

        AjaxMsg ajaxMsg = new AjaxMsg();
        ajaxMsg.setCode(AjaxMsg.FAILURE);

        for (String idstr : ids) {
            Integer id = Integer.parseInt(idstr);
            if (id != null && id != 0) {
                siteCoinService.deleteByPrimaryKey(id);
                ajaxMsg.setCode(AjaxMsg.SUCCESS);
            }
        }
        return ajaxMsg;
    }

}
